<?php

namespace maotool\captcha;

class CaptchaData
{

    protected $code;

    protected $bytes;

    protected $expire;

    public function __construct($bytes, $code)
    {
        $this->code = $code;
        $this->bytes = $bytes;
        $this->expire = time();
    }

    /**
     * @return string
     */
    public function toBase64()
    {
        return 'data:image/jpeg;base64,' . base64_encode($this->bytes);
    }

    /**
     * @return string
     */
    public function toBytes()
    {
        return $this->bytes;
    }

    /**
     * @param $code
     * @return bool
     */
    public function verify($code)
    {
        if (time() - $this->getExpire() > 60) {
            return false;
        }
        return strtolower($code) === strtolower($this->code);
    }

    /**
     * @return mixed
     */
    public function getCode()
    {
        return $this->code;
    }

    /**
     * @return mixed
     */
    public function getExpire()
    {
        return $this->expire;
    }

}